﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using LayerData.DataBaseManager;
using Npgsql;
using LayerEntity.Sistema;
using LayerEntity.Creditos;
using LayerEntity.Caja;

namespace LayerData.Creditos
{
  public  class DCreditos
    {
       #region Variables
        readonly string mensajeExecuteReader = "Error al Recuperar #replace, revise lo Siguiente: ";
        readonly IPostgreSqlConnectionInfo connectionInfo;
        public IPostgreSqlConnectionInfo CurrentConection { get; set; }
        #endregion
        #region Contructores
        public DCreditos()
        {
            this.connectionInfo = ObjectFactory.CreateConnectionInfo();
        }

        public DCreditos(IPostgreSqlConnectionInfo con)
        {
            this.connectionInfo = con;
            this.CurrentConection = con;
        }

        #endregion

        #region Mantenimientos
        public object[] MantenimientoAnticipos(ECreditos Ecred, EMovientoCaja Emov, EnumOperaciones operacion)
        {
            object obj;
            object[] Msg = null; ;
            string[] result = null;
            bool res = false;
            NpgsqlConnection con = null;
            try
            {
                con = this.CurrentConection.GetPostgreSqlConnection();
                NpgsqlTransaction tran = this.CurrentConection.GetTransaction(con);
                obj = PostgreSqlHelper.ExecuteScalar(this.CurrentConection, tran, "CREDITOS.ISP_GRABAR_CAB_ANTICIPO", Ecred.toSaveAnticipo((int)operacion));

                if (obj != null)
                {
                    result = (string[])obj;
                    Msg = (string[])obj;
                }
                else
                {
                    result = new string[] { "At-00" };
                }
                if (result[0] == "0")
                {
                    Ecred.Id_anticipo = result[1];
                    obj = PostgreSqlHelper.ExecuteScalar(this.CurrentConection, tran, "CREDITOS.ISP_GRABAR_DET_ANTICIPO", Ecred.toSaveDetAnticipo((int)operacion));

                    if (obj != null)
                    {
                        result = (string[])obj;
                    }
                    else
                    {
                        result = new string[] { "DAT-00" };
                    }
                    if (result[0].Equals("0"))
                    {
                        obj = PostgreSqlHelper.ExecuteScalar(this.CurrentConection, tran, "CREDITOS.ISP_GRABAR_CAB_CREDITO", Ecred.toSaveCredito((int)operacion));

                        if(obj!= null)
                        {
                            result = (string[])obj;
                        }
                        else
                        {
                            result = new string[] { "C-00" };
                        }
                        if (result[0].Equals("0"))
                        {
                            Ecred.Id_credito = result[1];
                            Ecred.Id_sucursal_credito = int.Parse(result[2]);
                            obj = PostgreSqlHelper.ExecuteScalar(this.CurrentConection, tran, "CREDITOS.ISP_GRABAR_CUOTA", Ecred.toSaveCuotaCredito((int)operacion));
                            if (obj != null)
                            {
                                result = (string[])obj;
                            }
                            else
                            {
                                result = new string[] { "C-00" };
                            }
                            string ID_CUOTA = null, NRO_LETRA = "", ID_CREDITO = null, ID_SUCURSAL_CREDITO = null;
                            if (result[0].Equals("0"))
                            {
                                ID_CUOTA = result[1];
                                ID_CREDITO = result[2];
                                ID_SUCURSAL_CREDITO = result[3];
                                object[] ObjectCuota = new object[]
                                        {
                                            ID_CUOTA,
                                            ID_CREDITO,
                                            ID_SUCURSAL_CREDITO
                                        };
                                obj = PostgreSqlHelper.ExecuteScalar(this.CurrentConection, tran, "CAJA.ISP_GRABAR_MOVCUOTA", Emov.ToMovcajaCuota((int)operacion,ObjectCuota));
                                if (obj != null)
                                {
                                    result = (string[])obj;
                                }
                                else
                                {

                                    result = new string[] { "MC-00" };

                                }
                                if (result[0] == "0")
                                {
                                    res = true;
                                }
                                else
                                {
                                    this.CurrentConection.CancelarConexionTrans(this.CurrentConection.GetPostgreSqlConnection());
                                    res = false;
                                }
                                //res = true;
                            }
                            else {
                                this.CurrentConection.CancelarConexionTrans(this.CurrentConection.GetPostgreSqlConnection());
                                res = false;
                            }
                            //res = true;
                        }
                        else
                        {
                            this.CurrentConection.CancelarConexionTrans(this.CurrentConection.GetPostgreSqlConnection());
                            res = true;
                        }
                    }
                }
                else
                {
                    this.CurrentConection.CancelarConexionTrans(this.CurrentConection.GetPostgreSqlConnection());
                    res = false;
                }
                if (res == true)
                {
                    this.CurrentConection.CommintTransaction(tran);
                }
                
            }
            catch (NpgsqlException ex)
            {
                result[0] = ex.Message;
                Console.Write(ex.Message);
                this.CurrentConection.CancelarConexionTrans(this.CurrentConection.GetPostgreSqlConnection());
                result = new string[] { ex.Message };
                throw ex;
            }
            finally
            {
                this.CurrentConection.CloseConnection(con);
            }
            return result;
        }

        public object[] UpdateCabAnticipos(ECreditos Ecred, EnumOperaciones operacion)
        {
            object obj;
            string[] result = null;
            bool res = false;
            NpgsqlConnection con = null;
            try
            {
                con = this.CurrentConection.GetPostgreSqlConnection();
                NpgsqlTransaction tran = this.CurrentConection.GetTransaction(con);
                obj = PostgreSqlHelper.ExecuteScalar(this.CurrentConection, tran, "CREDITOS.ISP_GRABAR_CAB_ANTICIPO", Ecred.toSaveAnticipo((int)operacion));

                if (obj != null)
                {
                    result = (string[])obj;
                }
                else
                {
                    result = new string[] { "AT-00" };
                }
                if (result[0] == "0")
                {
                    res = true;
                }
                else
                {
                    this.CurrentConection.CancelarConexionTrans(this.CurrentConection.GetPostgreSqlConnection());
                    res = false;
                }
                if (res == true)
                {
                    this.CurrentConection.CommintTransaction(tran);
                }
            }
            catch (Exception ex)
            {
                result[0] = ex.Message;
                Console.Write(ex.Message);
                this.CurrentConection.CancelarConexionTrans(this.CurrentConection.GetPostgreSqlConnection());
                result = new string[] { ex.Message };
                throw ex;
            }
            finally
            {
                 this.CurrentConection.CloseConnection(con);
            
            }
            return result;
        }
        public object[] RegisterAmortizaCuota(ECreditos Ecred, EMovientoCaja Emov, EnumOperaciones operacion)
        {
            object obj;

            string[] result = null;
            bool res = false;
            NpgsqlConnection con = null;
            try
            {
                object[] ObjectCuota = new object[]
                                        {
                                            Ecred.IdCuota,
                                            Ecred.Id_credito,
                                            Ecred.Id_sucursal_credito
                                        };

                con = this.CurrentConection.GetPostgreSqlConnection();
                NpgsqlTransaction tran = this.CurrentConection.GetTransaction(con);
                obj = PostgreSqlHelper.ExecuteScalar(this.CurrentConection, tran, "CAJA.ISP_GRABAR_MOVCUOTA", Emov.ToMovcajaCuota((int)operacion, ObjectCuota));
                if (obj != null)
                {
                    result = (string[])obj;
                }
                else
                {

                    result = new string[] { "MC-00" };

                }
                if (result[0] == "0")
                {
                    res = true;
                }
                else
                {
                    this.CurrentConection.CancelarConexionTrans(this.CurrentConection.GetPostgreSqlConnection());
                    res = false;
                }

                if (res==true)
                {
                    this.CurrentConection.CommintTransaction(tran);
                }
            }
            catch (NpgsqlException ex)
            {
                result[0] = ex.Message;
                Console.Write(ex.Message);
                this.CurrentConection.CancelarConexionTrans(this.CurrentConection.GetPostgreSqlConnection());
                result = new string[] { ex.Message };
                throw       ex;
            }
            finally
            {
                 this.CurrentConection.CloseConnection(con);
            
            }
            return result;
        }
        
        #endregion
        #region Consultas
        public DataTable getCtasporCobrar(ESistema obj)
        {
            DataTable dtb;
            NpgsqlConnection con = null;

            AccesoBD db = new AccesoBD();
            try
            {
                con = this.CurrentConection.GetPostgreSqlConnection();
                dtb = db.EjecutaModelView("creditos.v_listar_ctas_xcobrar", new object[] { obj.Condicion }, con);
            }
            catch (NpgsqlException ex)
            {
                this.CurrentConection.CloseConnection(con);
                Console.Write(string.Format("{0}/{1}", ex.Message, ex.Source));
                DataBaseUtils.PrintMessajeError(this.mensajeExecuteReader.Replace("#replace", " Los Productos"), ex);
                return null;
            }
            finally
            {
                this.CurrentConection.CloseConnection(con);
            }
            return dtb;
        }


        public DataTable getCuotasPorCobrar(ESistema obj)
        {
            DataTable dtb;
            NpgsqlConnection con = null;

            AccesoBD db = new AccesoBD();
            try
            {
                con = this.CurrentConection.GetPostgreSqlConnection();
                dtb = db.EjecutaModelView("creditos.V_LISTAR_CUOTAS", new object[] { obj.Condicion }, con);
            }
            catch (NpgsqlException ex)
            {
                this.CurrentConection.CloseConnection(con);
                Console.Write(string.Format("{0}/{1}", ex.Message, ex.Source));
                DataBaseUtils.PrintMessajeError(this.mensajeExecuteReader.Replace("#replace", " Las cuotas"), ex);
                return null;
            }
            finally
            {
                this.CurrentConection.CloseConnection(con);
            }
            return dtb;
        }
        public DataTable getCabAnticipos(ESistema obj)
        {
            DataTable dtb;
            NpgsqlConnection con = null;

            AccesoBD db = new AccesoBD();
            try
            {
                con = this.CurrentConection.GetPostgreSqlConnection();
                dtb = db.EjecutaModelView("creditos.v_listcab_anticipo", new object[] { obj.Condicion }, con);
            }
            catch (NpgsqlException ex)
            {
                this.CurrentConection.CloseConnection(con);
                Console.Write(string.Format("{0}/{1}", ex.Message, ex.Source));
                DataBaseUtils.PrintMessajeError(this.mensajeExecuteReader.Replace("#replace", " Las cuotas"), ex);
                return null;
            }
            finally
            {
                this.CurrentConection.CloseConnection(con);
            }
            return dtb;
        }
        public DataTable getCronogramaPagos(ESistema obj)
        {
            DataTable dtb;
            NpgsqlConnection con = null;

            AccesoBD db = new AccesoBD();
            try
            {
                con = this.CurrentConection.GetPostgreSqlConnection();
                dtb = db.EjecutaModelView("creditos.V_LISTAR_CREDITO_CRONOGRAMA", new object[] { obj.Condicion }, con);
            }
            catch (NpgsqlException ex)
            {
                this.CurrentConection.CloseConnection(con);
                Console.Write(string.Format("{0}/{1}", ex.Message, ex.Source));
                DataBaseUtils.PrintMessajeError(this.mensajeExecuteReader.Replace("#replace", " Las cuotas"), ex);
                return null;
            }
            finally
            {
                this.CurrentConection.CloseConnection(con);
            }
            return dtb;
        }
        #endregion

    }
}
